﻿using System;
using System.Collections;
using System.Xml;
using System.Threading;
using System.Globalization;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.ManagedCode;

namespace InfoJetSoft.Service.Worker
{
	
	class FormBuildHTMLDataWorker
	{
		//构造infojet.js处理时需要的数据.
		//这里是的数据是不需要再提交回页面的.但是能由xdoc.aspx的输出修改.
		public static void  buildHTMLData(Form form)
		{
			XmlDocument doc = form.HtmlDoc;
			XmlElement root = doc.DocumentElement;
			XmlElement dataElement = doc.CreateElement("div");
			dataElement.SetAttribute("id", JetConstant.DataId);
			dataElement.SetAttribute("style", "display:none;");
			
			root.AppendChild(dataElement);
			
			string xdPrefix = form.XdPrefix;
			if (xdPrefix.Length > 0)
			{
				xdPrefix = xdPrefix + ":";
			}
			XmlElement xdPrefixElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_XD_PREFIX, xdPrefix);
			dataElement.AppendChild(xdPrefixElement);

			XmlElement shortDatePatternElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_SHORT_DATE_PATTERN, Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern);
			dataElement.AppendChild(shortDatePatternElement);

			XmlElement longDatePatternElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_LONG_DATE_PATTERN, Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern);
			dataElement.AppendChild(longDatePatternElement);

			XmlElement yearMonthPatternElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_YEAR_MONTH_PATTERN, Thread.CurrentThread.CurrentCulture.DateTimeFormat.YearMonthPattern);
			dataElement.AppendChild(yearMonthPatternElement);
			
			string listIdArray = FormUtil.buildListValue(form.ListIdList);
			XmlElement listIdArrayElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_LIST_ID_ARRAY, listIdArray);
			dataElement.AppendChild(listIdArrayElement);
			
			string noBreakIdArray = FormUtil.buildListValue(form.NoBreakIdList);
			XmlElement noBreakIdArrayElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_NOBREAK_ID_ARRAY, noBreakIdArray);
			dataElement.AppendChild(noBreakIdArrayElement);
			
			string rootElementId = form.XmlDoc.DocumentElement.GetAttribute(JetConstant.JetId);
			XmlElement rootIdElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_ROOT_ELEMENT_ID, rootElementId);
			dataElement.AppendChild(rootIdElement);

			//xdoc_data_error是一个容器,Schema,Condition,Program都是记录在xdoc_data_error中的.
			XmlElement errorsElement = FormUtil.createHiddenDiv( doc, JetConstant.XDOC_DATA_ERROR, "" );
			dataElement.AppendChild(errorsElement);

			//输出Schema无效Node的定义到xdoc_data_error中。
			foreach( FieldError error in form.FieldErrorList )
			{
				if( error.ErrorType.Equals( FieldError.SchemaError ) )
				{
					XmlElement schemaInvalidateElement = FormUtil.createHiddenDiv( doc );
				
					schemaInvalidateElement.SetAttribute( "type", "schema" );
					schemaInvalidateElement.SetAttribute( JetConstant.JetId, error.JetId );

					errorsElement.AppendChild( schemaInvalidateElement );
				}
			}

			//输出Condition无效Node的定义到xdoc_data_error中。
			foreach( FieldError error in form.FieldErrorList )
			{
				if( error.ErrorType.Equals( FieldError.ConditionError ) )
				{
					XmlElement errCondInvalidateElement = FormUtil.createHiddenDiv( doc );
				
					errCondInvalidateElement.SetAttribute( "type", "condition" );
					errCondInvalidateElement.SetAttribute( JetConstant.JetId, error.JetId );
					errCondInvalidateElement.InnerText = error.Description;

					errorsElement.AppendChild( errCondInvalidateElement );
				}
			}

			//输出Program无效Node的定义到xdoc_data_error中。
			foreach( FieldError error in form.FieldErrorList )
			{
				if( error.ErrorType.Equals( FieldError.ProgramError ) )
				{
					XmlElement errorElement = FormUtil.createHiddenDiv( doc );

					errorElement.SetAttribute( "type", "program" );
					errorElement.SetAttribute( JetConstant.JetId, error.JetId );
					errorElement.InnerText = error.Description;

					errorsElement.AppendChild(errorElement);
				}
			}

			//输出菜单的Html内容,因为这个菜单不会被修改,而且包含所有视图的菜单,所以只需要在打开表单时输出.
			//在infojet.js 的InfoJet_Init中,这些内容将被作为body的直接子元素添加,所以有些情况下可能还是需要在body onload情况下调用InfoJet_Init().
			//因为如果Xhtml被包裹在absolute布局里面时,不能将menu定义放在Xhtml里,否则它的位置是相对于那个absolute元素的.
			//直接放在body里,效果和没有absolute布局的情况一样.
			XmlElement menuElement = FormUtil.createHiddenDiv( doc, JetConstant.XDOC_DATA_MENU, "" );
			//CID 0284 //2009.05.10 //|| form.Command.Equals(JetConstant.CMD_UPDATE_View) || form.IsViewSwitched
			//注意,IsViewSwitched只是用来判断是否在表单处理过程中被指示切换视图.
			if( form.Command.StartsWith( JetConstant.CMD_BUILD_BY ) || form.Command.Equals(JetConstant.CMD_UPDATE_View) || form.IsViewSwitched )
			{
				//这里的判断和视图只读无关,视图只读的菜单相关处理在XSNManifestMenuParseWorker中处理.
				if( FormUtil.isReadonly( form ) )
				{
					//如果表单是只读的,删除XmlToEdit操作条目.
					//CID 0284 //2009.05.10 //form.View.ContextMenuHtml
					string readonlyMenuHtml = MenuUtil.format2ReadonlyMenu( form.View.ContextMenuHtml );
					//CID 0284 //2009.05.10 //End
					menuElement.SetAttribute( "html", readonlyMenuHtml );
				}
				else
				{
					//如果不是只读的,输出所有菜单.
					//CID 0284 //2009.05.10 //form.View.ContextMenuHtml
					menuElement.SetAttribute( "html", form.View.ContextMenuHtml );
					//CID 0284 //2009.05.10 //End
				}
			}
			else
			{
				menuElement.SetAttribute( "html", "" );
			}
			dataElement.AppendChild(menuElement);

            //CID 1171 //2012.02.24 //Begin
            if (form.DatePickerIdList != null)
            {
                string datePickerIdArray = FormUtil.buildListValue(form.DatePickerIdList);
                XmlElement datePickerIdArrayElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_DATEPICKER_ID_ARRAY, datePickerIdArray);
                dataElement.AppendChild(datePickerIdArrayElement);
            }
            //CID 1171 //2012.02.24 //End

			string textAreaIdArray = FormUtil.buildListValue(form.TextAreaIdList);
			XmlElement textAreaIdArrayElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_TEXTAREA_ID_ARRAY, textAreaIdArray);
			dataElement.AppendChild( textAreaIdArrayElement);
			
			ArrayList xmlToEditFieldList = form.XmlToEditFieldList;
			foreach (Field xmlToEditField in xmlToEditFieldList)
			{
				XmlElement xmlToEditFieldElement = FormUtil.createHiddenDiv(doc, xmlToEditField.Name, xmlToEditField.Value);
				dataElement.AppendChild(xmlToEditFieldElement);
			}
			
			XmlElement maxLengthListElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_MAX_LENGTH_LIST, form.MaxLengthValue);
			dataElement.AppendChild(maxLengthListElement);

			//输出ValidViewButtonList定义,在infojet.js中判断.
			if( form.Manifest.FormConfiguration.ValidViewButtonList != null )
			{
				string vqButtonList = FormUtil.buildListValue(form.Manifest.FormConfiguration.ValidViewButtonList);
				XmlElement vqButtonListElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_VALID_VIEW_BUTTON_LIST, vqButtonList);
				dataElement.AppendChild(vqButtonListElement);
			}

			//输出ValidViewXmlToEditList定义,在infojet.js中判断.
			if( form.Manifest.FormConfiguration.ValidViewXmlToEditList != null )
			{
				string vqXmlToEditList = FormUtil.buildListValue(form.Manifest.FormConfiguration.ValidViewXmlToEditList);
				XmlElement vqXmlToEditListElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_VALID_VIEW_XMLTOEDIT_LIST, vqXmlToEditList);
				dataElement.AppendChild(vqXmlToEditListElement);
			}

			//输出Xml内容到页面。
			if( form.Manifest.FormConfiguration.EnableXDocumentDOM )
			{
				XmlElement xmlElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_DATA_XML, "");
				xmlElement.SetAttribute( "xml", form.XmlDoc.OuterXml );
				dataElement.AppendChild(xmlElement);
			}

			if( form.Manifest.FormConfiguration.EnableScriptX || ConfigUtil.EnableScriptX()  )
			{
				View currentView = form.Manifest.getView(form.ViewId);
				if ( currentView != null )
				{
					XmlElement printSettingElement = FormUtil.createHiddenDiv(doc, JetConstant.XDOC_PrintSetting, "");
					if( currentView.PrintHeader != null )
					{
						printSettingElement.SetAttribute( "header", currentView.PrintHeader );
					}
					if( currentView.PrintFooter != null )
					{
						printSettingElement.SetAttribute( "footer", currentView.PrintFooter );
					}
					if( currentView.TopMargin >= 0 )
					{
						printSettingElement.SetAttribute( "topMargin", currentView.TopMargin + "" );
					}
					if( currentView.LeftMargin >= 0 )
					{
						printSettingElement.SetAttribute( "leftMargin", currentView.LeftMargin + "" );
					}
					if( currentView.BottomMargin >= 0 )
					{
						printSettingElement.SetAttribute( "bottomMargin", currentView.BottomMargin + "" );
					}
					if( currentView.RightMargin >= 0 )
					{
						printSettingElement.SetAttribute( "rightMargin", currentView.RightMargin + "" );
					}
					dataElement.AppendChild(printSettingElement);
				}
			}

			//输出MailEnvelope
			if( form.MailEnvelope != null && form.MailEnvelope.Visible )
			{
				XmlElement mailEnvelopeElement = FormUtil.createHiddenA(doc, JetConstant.XDOC_MailEnvelope);
				string href = "mailto:" + form.MailEnvelope.To;
				href += "?subject=" + form.MailEnvelope.Subject;
				href += "&cc=" + form.MailEnvelope.CC;
				href += "&bcc=" + form.MailEnvelope.BCC;
				mailEnvelopeElement.SetAttribute( "href", href );

				dataElement.AppendChild(mailEnvelopeElement);
			}
		}
	}
}